<?php
/**
 * 角色权限管理
 * 
 * @author:  will.lee <im.will.lee@gmail.com>
 * @version: 1.0
 */
class UserRoleAction extends AdminAction
{
    /**
     * 角色列表
     */ 
    public function index()
    {
        if(IS_GET)
        {
            $this->data = array_map('urldecode',$_GET);
        }
        // 检索条件
        $where = $this->initSearchOption();
        $Role  = D('Role');
        $count   = $Role->where($where)->count();
        $p       = $this->page($count,$this->data);
        $roles   = $Role->field(true)->where($where)->order('id DESC')->limit($this->pageLimit)->select();
        $this->assign(compact('roles','p'));
        $this->display();
    }
    
    /**
     * 角色添加
     */
    public function add()
    {
        if(IS_AJAX)
        {
            $Role = D('Role');
            // 关闭批量验证
            $Role->setValidate('save');
            $Role->patchValidate = false;
            if($Role->create($this->data))
            {
                if($Role->add())
                {
                    $this->ajaxFormReturn('数据保存成功!',1);
                }
                else
                {
                    $this->ajaxFormReturn('系统发生错误');
                }
            }
            else
            {
                $this->ajaxFormReturn($Role->getError());
            }
        }
        $this->display();
    }
    
    /**
     * 角色编辑
     */
    public function edit()
    {
        $show_info = array();
        $Role      = D('Role');
        if(IS_AJAX)
        {
            // 超级管理员不允许更新
            if($this->_post('id') == 1)
            {
                $this->ajaxFormReturn('参数错误');
            }
            // 更新冲突检测
            $this->checkUpdateConflict($Role);
            // 关闭批量验证
            $Role->setValidate('save');
            $Role->patchValidate = false;
            if($Role->create($this->data))
            {
                if($Role->save())
                {
                    $this->ajaxFormReturn('数据保存成功!',1);
                }
                else
                {
                    $this->ajaxFormReturn('系统发生错误');
                }
            }
            else
            {
                $this->ajaxFormReturn($Role->getError());
            }
        }
        else
        {
            $sid = $this->_get('rid');
            if($sid == '' || $sid == 1)
            {
                $show_info['message'] = '参数错误!';
                $this->error($show_info);
            }
            $where  = array('id' => $sid);
            $result = $Role->field(true)->where($where)->find();
            if(empty($result))
            {
                $show_info['message'] = '数据不存在或已被删除!';
                $this->error($show_info);
            }
            $this->assign(compact('result'));
        }
        $this->display();
    }
    
    /**
     * 角色删除
     */
    public function delete()
    {
        if(IS_AJAX)
        {
            $rids = $this->_post('id');
            if(empty($rids))
            {
                $this->ajaxFormReturn('请至少选择一项!');
            }
            if(in_array(1,$rids))
            {
                $this->ajaxFormReturn('不能删除超级管理员!');
            }
            $where  = array('id' => array('IN',$rids));
            $Role = D('Role');
            if($Role->where($where)->delete())
            {
                $this->ajaxFormReturn('删除成功!',1);
            }
            else
            {
                $this->ajaxFormReturn('系统发生错误');
            }
        }
    }
    
    /**
     * 权限设置
     */
    public function auth()
    {
        $RoleNode   = D('RoleNode');
        if(IS_AJAX)
        {
            $where = array('role_id' => $this->data['role_id']);
            $RoleNode->where($where)->delete();
            if(!empty($this->data['id']))
            {
                $add_list = array();
                foreach($this->data['id'] as $key => $val)
                {
                    $add_list[] = array('role_id' =>$this->data['role_id'],'node_id' => $val);
                }
                if($RoleNode->addAll($add_list))
                {
                    $this->ajaxFormReturn('数据保存成功!',1);
                }
                else
                {
                    $this->ajaxFormReturn('系统发生错误!');
                }
            }
        }
        $rid = $this->_get('rid');
        $show_info = array();
        if($rid == '' || $rid == 1)
        {
            $show_info['message'] = '参数错误!';
            $this->error($show_info);
        }
        $Role       = D('Role');
        $role_info  = $Role->field(true)->where(array('id'=>$rid))->find();
        if(empty($role_info))
        {
            $show_info['message'] = '数据不存在或已被删除!';
            $this->error($show_info);
        }
        $nodes      = $RoleNode->getRoleNodeIds($rid);
        $Node = D('Node');
        // 管理端
        $admin      = $Node->getAllSystemNodeGroup();
        // 前端
        $front      = $Node->getAllSystemNodeGroup(2);
        $this->assign(compact('role_info','admin','front','nodes'));
        $this->display();
    }
    
    /**
     * 检索条件组装
     */
     protected function initSearchOption()
     {
        $where = array();
        // 账号区分
        if(isset($this->data['role_type']) && $this->data['role_type'] != '')
        {
            $where['role_type'] = $this->data['role_type'];
        }
        return $where;
     }
}